package com.imooc.service.impl;


import com.imooc.mapper.*;
import com.imooc.pojo.BasicCode;
import com.imooc.pojo.Users;
import com.imooc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;


    @Override
    public boolean queryUseridIsExist(int userid) {
        Example e = new Example(Users.class);
        Example.Criteria c = e.createCriteria();

        c.andEqualTo("userid",userid);

        e.orderBy("userid").desc();

        List<Users> users = usersMapper.selectByExample(e);


        if(users.size() == 0){
            return false;
        }else {
            return true ;
        }

    }

    @Override
    public Users queryUserForLogin(int userid, String pwd) {

        Example e = new Example(Users.class);
        Example.Criteria c = e.createCriteria();

        c.andEqualTo("userid",userid);
        c.andEqualTo("pwd",pwd);
        e.orderBy("userid").orderBy("userid").desc();

        List<Users> users = usersMapper.selectByExample(e);

        if(users.size() == 0){
            return null;
        }else {
            return users.get(0) ;
        }
    }

    @Override
    public Users saveUser(Users users)  {

        int id = usersMapper.insertUser(users);
        users.setId(id);
        return users ;

    }

    @Override
    public BasicCode saveBasicCode(BasicCode basicCode) {
        int id = usersMapper.insertBasicCode(basicCode);
        basicCode.set_id(id);
        return basicCode ;
    }


    @Override
    public Users getUser(String username) {
       return usersMapper.getUsername(username);
    }

    @Override
    public BasicCode getBasicCode(int userid) {
        return usersMapper.getBasicCode(userid);
    }


}
